
package autoteam.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import autoteam.League;
import autoteam.tables.WeightEditorTableModel;

public class WeightEditor extends JDialog
{
    
    private static final long serialVersionUID = 1L;
    private static WeightEditor dialog = null;
    private static boolean val = true;
    WeightEditorTableModel parserModel = null;
    
    /**
     * Set up the dialog. The first argument can be null, but it really should
     * be a component in the dialog's controlling frame.
     */
    private static void initialize(JFrame frame, League doc)
    {
	dialog = new WeightEditor(frame, doc);
    }
    
    /**
     * Show the initialized dialog. The first argument should be null if you
     * want the dialog to come up in the center of the screen. Otherwise, the
     * argument should be the component on top of which the dialog should
     * appear.
     */
    public static boolean showDialog(JFrame frame, League doc)
    {
	if (dialog == null)
	    initialize(frame, doc);
	else
	    dialog.parserModel.setChildren(doc);
	dialog.setLocationRelativeTo(frame);
	dialog.setVisible(true);
	
	return val;
    }
    
    private WeightEditor(JFrame frame, League doc)
    {
	super(frame, "WeightEditor", true);
	
	// buttons
	JButton cancelButton = new JButton("Cancel");
	final JButton okButton = new JButton("OK");
	cancelButton.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent e)
	    {
		WeightEditor.dialog.setVisible(false);
		WeightEditor.val = false;
	    }
	});
	okButton.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent e)
	    {
		WeightEditor.dialog.setVisible(false);
		WeightEditor.val = true;
		parserModel.transfer();
	    }
	});
	getRootPane().setDefaultButton(okButton);
	
	// the editing table
	
	parserModel = new WeightEditorTableModel(doc);
	final JTable table = new JTable(parserModel);
	table.setPreferredScrollableViewportSize(new Dimension(500, 70));
	
	JScrollPane tableScroller = new JScrollPane(table);
	tableScroller.setPreferredSize(new Dimension(250, 80));
	// XXX: Must do the following, too, or else the scroller thinks
	// XXX: it's taller than it is:
	tableScroller.setMinimumSize(new Dimension(250, 80));
	tableScroller.setAlignmentX(LEFT_ALIGNMENT);
	
	// Create a container so that we can add a title around
	// the scroll pane. Can't add a title directly to the
	// scroll pane because its background would be white.
	// Lay out the label and scroll pane from top to button.
	JPanel tablePane = new JPanel();
	tablePane.setLayout(new BoxLayout(tablePane, BoxLayout.Y_AXIS));
	tablePane.add(Box.createRigidArea(new Dimension(0, 5)));
	tablePane.add(tableScroller);
	tablePane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	
	// Lay out the buttons from left to right.
	JPanel buttonPane = new JPanel();
	buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
	buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
	buttonPane.add(Box.createHorizontalGlue());
	buttonPane.add(okButton);
	buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
	buttonPane.add(cancelButton);
	
	// Put everything together, using the content pane's BorderLayout.
	Container contentPane = getContentPane();
	contentPane.add(tablePane, BorderLayout.CENTER);
	contentPane.add(buttonPane, BorderLayout.SOUTH);
	
	pack();
	
    }
}
